# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
from typing import List, Optional
from typing import ListNode
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
#交点不是数值相等，而是指针相等
        lenA,lenB=0,0

        cur=headA
        while cur!=None:
            cur=cur.next
            lenA+=1
        
        cur=headB
        while cur!=None:
            cur=cur.next
            lenB+=1

        curA,curB=headA,headB

        if lenA>lenB:
            curA,curB=curB,curA
            lenA,lenB=lenB,lenA
        
        for i in range(lenB-lenA):
            curB=curB.next

        while curA!=None:
            if curA==curB:
                return curA
            else:
                curA=curA.next
                curB=curB.next

        return None